﻿package fr.oz.view.profile.dragable.ui 
{
	import flash.display.GradientType;
	import flash.display.Graphics;
	import flash.display.Sprite;
	import flash.geom.Matrix;
	
	/**
	 * ...
	 * @author Fazermokeur
	 */
	public class CircularMinut extends Sprite
	{
		
		public var currentAngle:Number;
		public var isDowning:Boolean;
		
		private var COLOR:uint;
		private var RAD_OUT:uint;
		private var mat:Matrix;
		
		public function CircularMinut() 
		{
			RAD_OUT = 150;
			COLOR = 0xffffff;
			
			//currentAngle = -89;
			graphics.lineStyle(40, COLOR, 1);
			buttonMode = true;
			
		}
		
		public function traceCircle(angle:Number):void 
		{
			//if (isDowning) currentAngle ++;
			//else if(currentAngle > -89)currentAngle --;
			//trace(currentAngle);
			
			currentAngle = angle - 90;
			
			var RAD_IN:int = 115	;
			
			graphics.clear();
			/*mat = new Matrix();
			mat.createGradientBox(270, 270,0,-360+angle,-150);
			graphics.beginGradientFill(GradientType.LINEAR,
			//[0xC7B299, 0xD4145A],
			[0wffffff, 0xffffff],
			[.6, .6],
			[0, 255],
			mat)*/;
			
			graphics.beginFill(0xffffff);
			graphics.moveTo(0, 0);
			graphics.lineTo(0, -RAD_OUT);
			
			
			drawCircleSegment(this.graphics, RAD_OUT, -90, currentAngle, 0, 0, "degrees");
			graphics.lineTo(0, 0);
		}
		
		/* Draw Segment of Circle */
		private function drawCircleSegment( g : Graphics, radius: Number, startAngle: Number, endAngle: Number, x : Number = 0, y: Number = 0, unit:String = "radians") : void
		{		
			if(unit == "degrees")
			{
				startAngle = startAngle / 180 * Math.PI ;
				endAngle = endAngle / 180 * Math.PI ;
			}
			
			var segAngle: Number = Math.PI / 10;
			var angle: Number = startAngle;
			var angleMid: Number;
			var arc: Number = endAngle - angle;
			
			if( arc == 0 ) return;
			//while ( arc < 0 ) arc += Math.PI * 2;
			
			var segs: Number = Math.ceil( arc / segAngle );
			var rest : Number = segAngle - ( segs * segAngle ) + arc;
			var theta: Number = segAngle / 2;
			var cosTheta: Number = Math.cos( theta );
			var ax: Number = Math.cos( angle ) * radius;
			var ay: Number = Math.sin( angle ) * radius;
			var bx: Number, by: Number, cx: Number, cy: Number;
			
			g.moveTo( ax + x, ay + y );
			for( var i: Number = 0 ; i < Math.abs(segs) - 1 ; i++ )
			{
				angle += theta*2;
				angleMid = angle - theta;
				bx = x + Math.cos( angle ) * radius;
				by = y + Math.sin( angle ) * radius;
				cx = x + Math.cos( angleMid ) * ( radius / cosTheta );
				cy = y + Math.sin( angleMid ) * ( radius / cosTheta );
				g.curveTo( cx, cy, bx, by );
			}
			angle += rest;
			angleMid = angle -rest/2;
			bx = x + Math.cos( angle ) * radius;
			by = y + Math.sin( angle ) * radius;
			cx = x + Math.cos( angleMid ) * ( radius / Math.cos( rest/2)  );
			cy = y + Math.sin( angleMid ) * ( radius / Math.cos( rest/2) );
			g.curveTo( cx, cy, bx, by );
		}
	}
	
}